#include "dominion.c"
#include "assert.h"
#include "stdio.h"
#include "rngs.c"

void checkDrawCard( struct gameState *state );

int main(){
	int *cards = kingdomCards(7,8,9,10,11,12,13,14,15,16);
	struct gameState *state = newGame();
	
	initializeGame(4, cards, -1, state);
	
	for (int i = 0; i < 20; i++){
		for (int j = 0; j < 5; j++){
			checkDrawCard( state );
		}
		endTurn( state ); // This is needed to get cards into the discard for total coverage
	}
	
	return 0;
}

void checkDrawCard( struct gameState *state){
	int player = (int)(Random() * 3.99); // Pick a random player from 0 to 3

	int deck = state->deckCount[player];
	int discard = state->discardCount[player];
	int hand = state->handCount[player];
	
	if ( !drawCard( player, state ) ) {
		// The discard should've been shuffled, then a card drawn if the deck was empty
		if ( deck == 0 && discard != state->deckCount[player] + 1) {
				printf("Discard shuffled into deck, but size mismatch! Old discard size: %d. New deck size after draw: %d", discard, state->deckCount[player]);
			assert(0);
		}
		// If the deck isn't empty, then it should've shrunk by one
		if ( deck > 0 && deck != state->deckCount[player] + 1 ) {
			printf("Card was drawn, but deck size is wrong! Old size: %d. New size: %d\n", deck, state->deckCount[player]);
			assert(0);
		}
		// Hand should've gained a card
		if ( hand != state->handCount[player] - 1) {
			printf("Card was drawn, but hand size is wrong! Old size: %d. New size: %d\n", hand, state->handCount[player]);
			assert(0);
		}
	} else {
		// All piles should be the same still
		if ( deck != state->deckCount[player] ) {
			printf("Card wasn't drawn, but deck size changed! Old size: %d. New size: %d\n", deck, state->deckCount[player]);
			assert(0);
		}
		if ( discard != state->discardCount[player] ) {
			printf("Card wasn't drawn, but discard size changed! Old size: %d. New size: %d\n", discard, state->discardCount[player]);
			assert(0);
		}
		if ( hand != state->handCount[player] ) {
			printf("Card wasn't drawn, but hand size changed! Old size: %d. New size: %d\n", hand, state->handCount[player]);
			assert(0);
		}
	}

	int total = deck + discard + hand;
	int newTotal = state->deckCount[player] + 
		state->discardCount[player] + 
		state->handCount[player];
	
	// Total sum of cards in deck, discard and hand is invariant with drawCard()
	if ( total != newTotal ) {
		printf("Total number of cards changed! Previous total: %d. New total: %d", total, newTotal);
		assert(0);	
	}
}




















